import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  menuList: [],
  newMenu: {}
};

const getters = {
  menuList: state => state.menuList,
  newMenu: state => state.newMenu
};

const actions = {
  //获取用户菜单
  getUserMenus({ commit }, para) {
    return Api.menuListUsingPOST(para).then(ret => {
      console.log("buttonList: " + ret.data.buttonList);
      window.sessionStorage.setItem("roleKeys", ret.data.buttonList);
      commit(types.UPDATE_USER_MENU_LIST, ret.data.menusList);
      return ret;
    });
  },
  //根据应用获取
  resourceTreeUsingPOSTS({ commit }, param) {
    return Api.resourceBaseTree(param).then(ret => {
      commit(types.UPDATE_MENU_LIST, ret.data);
      return ret;
    });
  },
  //权限根据应用资源列表（树状）
  resourceBaseTreeUsingPOSTS({ commit }, param) {
    return Api.resourceBaseTreeUsingPOST(param).then(ret => {
      commit(types.UPDATE_MENU_LIST, ret.data);
      return ret;
    });
  },

  // 添加权限资源
  resSaveUsingPOSTS({ commit }, payload) {
    return Api.addOrUpdateResource(payload).then(ret => {
      return ret;
    });
  },

  //编辑权限资源
  resEditUsingPOSTS({ commit }, payload) {
    return Api.resEditUsingPOST(payload).then(ret => {
      return ret;
    });
  },

  //删除权限资源
  resDelUsingPOSTS({ commit }, param) {
    return Api.deleteResourceById(param).then(ret => {
      return ret;
    });
  },
  //权限资源详情
  resViewUsingPOSTS({ commit }, param) {
    return Api.getResourceById(param).then(ret => {
      commit(types.UPDATE_NEW_MENU, ret.data);
      return ret;
    });
  }
};

const mutations = {
  [types.UPDATE_MENU_LIST](state, payload) {
    state.menuList = payload;
  },
  [types.UPDATE_NEW_MENU](state, payload) {
    state.newMenu = payload;
  },
  [types.INIT_NEW_MENU](state) {
    state.newMenu = {
      alias: "",
      name: "",
      icon: "",
      type: "",
      parentId: "",
      url: ""
    };
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
